﻿var VoteClient = function(rHd){
    var __hd = (typeof(rHd)=="string")?rHd:"voteClient";
    var __data = null;
    var __requestUrl = null;
    var __templateKey = null;
    var __ajax = null;
    var __box = null;
    var __template = null;
    var __showLoading = function(){
        __box.innerHTML = '<div style="	background-image:url(/cellula_client/Controls/images/loading.gif);background-position:center center;background-repeat:no-repeat;width:40px;height:40px;overflow:hidden;">&nbsp;</div>';   
    };
    var __showLoaded = function(){
        __box.innerHTML = '';   
    };
    
    var __gc = function(){
        __data = __ajax = __template = null;
    }
    
    var __buildContent = function(){
        var _tl = __template;
        _tl = _tl.replace(/\$VoteKey/gi,__data.VoteKey);
        _tl = _tl.replace(/\$VoteTitle/gi,__data.VoteTitle);
        _tl = _tl.replace(/\$VoteDesc/gi,__data.VoteDesc);
        
        // action -------------------------------------------
        var item = null;
        var _str = new Array();
        var _checkType = (__data.IsMultiSelect=="1")?"checkbox":"radio";
        for(var i=0;i<__data.Items.length;i++){
            item = __data.Items[i];
            _str.push('<div class="vote_r_item">');
            _str.push('<input type="'+_checkType+'" value="'+item.ItemKey+'" name="VoteItemCheckGroup" />');
            _str.push(item.ItemContent);
            _str.push('</div>');
        }
        _tl = _tl.replace(/\$VoteItemList/gi,_str.join(''));
        // result -------------------------------------------
        _str = new Array();
        var totalCount = 0;
        for(var i=0;i<__data.Items.length;i++){
            item = __data.Items[i];
            if(!isNaN(parseInt(item.Score))){
                totalCount += parseInt(item.Score);
            }
        }
        _tl = _tl.replace(/\$TotalCount/gi,totalCount);
        var rate = 5/Math.ceil(totalCount/100);
        var percent = 0;
        for(var i=0;i<__data.Items.length;i++){
            item = __data.Items[i];
            percent = (item.Score/totalCount)*100+"";
            _str.push('<div class="vote_r_item">');
            _str.push('<span class="vote_num">'+(i+1)+'、</span>');
            _str.push(item.ItemContent);
            _str.push('</div>');
            _str.push('<table cellspacing="0" cellpadding="0" border="0">');
            _str.push('<tr><td>');
            _str.push('<div class="vote_score_bg" style="width:'+((totalCount*rate)+2)+'px">');
            _str.push('<div class="vote_score_bar" style="width:'+(item.Score*rate)+'px"></div>');
            _str.push('</div>');
            _str.push('</td><td>');
            _str.push('<div class="vote_score" >'+(percent.toFormatFloat(0,1))+'%，'+(item.Score)+'</div>');
            _str.push('</td></tr>');
            _str.push('</table>');
        }
        _tl = _tl.replace(/\$VoteResultList/gi,_str.join(''));
        totalCount = null;
        //-------------------------------------------------
        _str = null;
                
        _tl = _tl.replace(/\$FuncHandler/gi,__hd);
        _tl += '<a href="" id="'+__hd+'_VoteLinkButton" target="_blank" style="display:none"></a>';
        __box.innerHTML = _tl;
        _tl = null;
        __gc();
    };
    
    this.doVote = function(rVoteKey){
        var cl = $("@VoteItemCheckGroup");
        var keys = new Array();
        if(cl!=null){
            for(var i=0;i<cl.length;i++){
                if(cl[i].checked){
                    keys.push(cl[i].value);
                }
            }
        }
        cl = null;
        if(keys.length<1){
            alert("您什么都没有选则");
            return;
        }
        var keyStr = (keys.length==1)?keys:keys.join(',');
        var url = "Biz/Info/Vote.aspx?id="+rVoteKey+"&items="+keyStr;
        var link = $("#"+__hd+"_VoteLinkButton");
        link.href = url;
        link.click();
    };
    
    var __loadData = function(){
        __ajax = new JskitXmlHttpAction(__requestUrl,__hd+".__loadDataCallback","text");
        __showLoading();
    };
    var __loadTemplate = function(){
        __ajax = new JskitXmlHttpAction("Ws/_Template.aspx?id="+__templateKey,__hd+".__loadTemplateCallback","text");
        __showLoading();
    };
    
    this.__loadTemplateCallback = function(tl){
        __ajax = null;
        __showLoaded();
        __template = tl;
        try{
            __buildContent();
        }catch(e){
            __box = e.message;
        }
    };
    this.__loadDataCallback = function(json){
        __ajax = null;
        __showLoaded();
        if(json=="0"){
            __box.innerHTML = "活动即将开始";
        }else if(json=="9"){
            __box.innerHTML = "活动已经结束";
        }else{
            try{
                eval("__data = "+json+";");
            }catch(e){
                __box = e.message;
            }
            if(__data==null)return;
            __loadTemplate();                        
        }
    };
    
    this.deploy = function(rBoxId,rRequestUrl,rTemplateId){
        __box = $("#"+rBoxId);
        __requestUrl = rRequestUrl;
        __templateKey = rTemplateId;
        __loadData();
    };
};